package com.example.back.service;

import com.example.back.entity.Comment;
import com.example.back.entity.Notice;
import com.example.back.entity.otd.CommentDTO;
import com.example.back.mapper.CommentMapper;
import com.example.back.mapper.NoticeMapper;
import com.example.back.mapper.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private PostMapper postMapper;
    // 新增评论（支持回复功能）
    public int addComment(Comment comment) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment.setTime(sdf.format(new Date())); // 生成当前时间

        int result = commentMapper.addComment(comment);
        if (result > 0) {
            // 发送通知（区分“评论帖子”和“回复评论”）
            Notice notice = new Notice();
            notice.setTriggerUserId(comment.getUserId()); // 触发者（评论者）
            notice.setPostId(comment.getPostId());
            notice.setTime(sdf.format(new Date()));
            notice.setType("comment");

            if (comment.getParentId() == null) {
                // 情况1：评论帖子 → 通知帖子作者
                int postAuthorId = postMapper.queryPostById(comment.getPostId()).getUserId();
                notice.setUserId(postAuthorId); // 被通知者（帖子作者）
                notice.setText("用户" + comment.getUserId() + "评论了你的帖子");
            } else {
                // 情况2：回复评论 → 通知被回复的评论作者
                Comment parentComment = commentMapper.queryCommentById(comment.getParentId());
                int repliedUserId = parentComment.getUserId(); // 被回复者ID
                notice.setUserId(repliedUserId); // 被通知者（被回复的评论作者）
                notice.setText("用户" + comment.getUserId() + "回复了你的评论");
            }
            noticeMapper.addNotice(notice);
        }
        return result;
    }
    public int deleteComment(Integer id)
    {
        return commentMapper.deleteComment(id);
    }
    public int updateComment(Comment comment)
    {
        return commentMapper.updateComment(comment);
    }
    public Comment queryCommentById(Integer id)
    {
        return commentMapper.queryCommentById(id);
    }
    public ArrayList<Comment> queryComments(Comment comment)
    {
        return commentMapper.queryComments(comment);
    }

    public ArrayList<CommentDTO> queryCommentsByPostId(Integer postId)
    {
        return commentMapper.queryCommentsByPostId(postId);
    }




    // 查询帖子的所有评论（包含嵌套回复）
    public List<CommentDTO> queryCommentsWithReplies(Integer postId) {
        // 1. 查询一级评论（直接对帖子的评论）
        List<CommentDTO> firstLevelComments = commentMapper.queryFirstLevelComments(postId);

        // 2. 为每个一级评论查询子评论（回复），组装成嵌套结构
        for (CommentDTO firstComment : firstLevelComments) {
            List<CommentDTO> childComments = commentMapper.queryChildComments(firstComment.getCommentId());
            firstComment.setChildren(childComments); // 绑定子评论
        }
        return firstLevelComments;
    }
}
